package edu.wong.pro.serviceImpl;

import java.util.List;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.sql.Alias;

import edu.wong.pro.dao.ActivityListDAO;
import edu.wong.pro.dao.PlanActivityDAO;
import edu.wong.pro.iservice.IPlanActivity;
import edu.wong.pro.model.ActivityList;
import edu.wong.pro.model.PlanActivity;
import edu.wong.pro.other.OtherService;

public class PlanActivityService implements IPlanActivity{

	//添加学期计划活动
	@Override
	public int addPlanActivity(String year, String semester, String theme) {
		// TODO Auto-generated method stub
		//如果当前学期计划活动已经添加，返回0
		if(isadd(year, semester)){
			return 0;
		}
		ActivityListDAO dao = new ActivityListDAO();
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		ActivityList list = new ActivityList(year, semester, theme);
		dao.save(list);
		transaction.commit();
		session.close();
		return list.getListId();
	}

	@Override
	public int savePlanActivity(String listID, String date, String theme,
			String addr) {
		// TODO Auto-generated method stub
		PlanActivityDAO dao = new PlanActivityDAO();
		Session session = dao.getSession();
		Transaction transaction = session.beginTransaction();
		PlanActivity planActivity = new PlanActivity();
		planActivity.setListId(Integer.parseInt(listID));
		planActivity.setPlanAddr(addr);
		planActivity.setPlanDate(date);
		planActivity.setPlanTheme(theme);
		dao.save(planActivity);
		transaction.commit();
		session.close();
		return 0;
	}

	@Override
	public JSONArray getPlanActivityList() {
		// TODO Auto-generated method stub
		ActivityListDAO dao = new ActivityListDAO();
		List<ActivityList> list = dao.findAll();
		JSONArray array = JSONArray.fromObject(list);
		return array;
	}

	@Override
	public JSONArray getPlanActivity(int listId) {
		// TODO Auto-generated method stub
		PlanActivityDAO dao = new PlanActivityDAO();
		List<PlanActivity> list = dao.findByProperty("listId", listId);
		return JSONArray.fromObject(list);
	}
	
	//判断当前学期计划活动是否已经添加
	private boolean isadd(String year,String semester){
		ActivityList list = new ActivityList();
		list.setYear(year.trim());
		list.setSemester(semester.trim());
		ActivityListDAO dao = new ActivityListDAO();
		List<ActivityList> lists = dao.findByExample(list);
		if(lists.size()>0){
			return true;
		}else {
			return false;
		}
	}

	//获取当月计划活动在主页显示
	@Override
	public JSONArray getCurrentPlanAct() {
		// TODO Auto-generated method stub
		String currentTime = OtherService.getCuttentYearMouth();
		PlanActivityDAO dao = new PlanActivityDAO();
		return JSONArray.fromObject(dao.findByProperty("planDate", currentTime));
	}

	//通过ID查找计划活动
	@Override
	public JSONObject getPlanActById(String planId) {
		// TODO Auto-generated method stub
		PlanActivityDAO dao = new PlanActivityDAO();
		return JSONObject.fromObject(dao.findById(Integer.parseInt(planId)));
	}
}
